package com.itc.bi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itc.bi.constant.KafkaConstant;
import com.itc.bi.dto.kafka.DataSetDTO;
import com.itc.bi.entity.*;
import com.itc.bi.mapper.DataSetColumnMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.itc.bi.service.impl
 * @Author: hecaiy
 * @CreateTime: 2024-08-07  16:59
 * @Description: TODO 编辑字段类型 刷新表数据
 * @Version: 1.0
 */
@Slf4j
@Component
public class EditColumnProcessDataService {

    @Autowired
    private DataSetColumnMapper dataSetColumnMapper;
    @Autowired
    private SyncEditColumnProcessService syncEditColumnProcessService;

    @KafkaListener(groupId = "bi_data_set_edit",topics = KafkaConstant.KAFKA_TOPIC_DATA_SET_COLUMN_EDIT)
    public void editColumnProcessData(ConsumerRecord record) {
        try{
            String message = (String) record.value();
            DataSetDTO dataSetDTO = JSON.parseObject(message, DataSetDTO.class);
            List<DataSetColumnEntity> dataSetColumnEntities = dataSetColumnMapper.selectList(Wrappers.<DataSetColumnEntity>lambdaQuery()
                    .eq(DataSetColumnEntity::getDataSetId,dataSetDTO.getDtId()).orderByAsc(DataSetColumnEntity::getColumnPosition));
            if(CollectionUtil.isEmpty(dataSetColumnEntities) || dataSetColumnEntities.size()==0){
                return;
            }
            syncEditColumnProcessService.processDataSetCode(dataSetDTO,dataSetColumnEntities);
        }catch (Exception e){}
    }
}
